﻿using System.Collections.Generic;
using System.Linq;
using WebStickyNotes.Contracts;
using WebStickyNotes.Contracts.Interfaces;

namespace WebStickyNotes.Helpers
{
    public static class FilteredDataHelper
    {
        public static List<TaskProject> GetFilteredTaskProjectsForUser(IWebStickyNotesDataAccess dataAccess, TaskPerson currentUser)
        {
            var taskProjects = dataAccess.GetTaskProjectsForPerson(currentUser.Id);

            if (taskProjects.Count == 0)
            {
                taskProjects = dataAccess.GetAllTaskProjects();
            }

            return taskProjects;
        }

        public static List<TaskStream> GetFilteredTaskStreamsForUser(IWebStickyNotesDataAccess dataAccess, TaskPerson currentUser)
        {
            var taskProjects = dataAccess.GetTaskProjectsForPerson(currentUser.Id);
            var taskStreams = dataAccess.GetAllTaskStreams();

            if (taskProjects.Count > 0)
            {
                var filteredStreams = new List<TaskStream>();
                foreach (var stream in taskStreams)
                {
                    var id = stream.TaskProjectId;
                    if (taskProjects.Where(project => project.Id.Equals(id)).Count() > 0)
                    {
                        filteredStreams.Add(stream);
                    }
                }

                return filteredStreams;
            }

            return taskStreams;
        }

        public static List<TaskStream> GetFilteredTaskStreamsForProjectAndUser(IWebStickyNotesDataAccess dataAccess, TaskPerson currentUser, int projectId)
        {
            var taskProjects = dataAccess.GetTaskProjectsForPerson(currentUser.Id);
            var taskStreams = dataAccess.GetStreamsByProject(projectId);

            if (taskProjects.Count > 0)
            {
                var filteredStreams = new List<TaskStream>();
                foreach (var stream in taskStreams)
                {
                    var id = stream.TaskProjectId;
                    if (taskProjects.Where(project => project.Id.Equals(id)).Count() > 0)
                    {
                        filteredStreams.Add(stream);
                    }
                }

                return filteredStreams;
            }

            return taskStreams;
        }

        public static List<TaskDetail> GetFilteredTaskDetailsForUser(IWebStickyNotesDataAccess dataAccess, TaskPerson currentUser, List<TaskDetail> taskDetails)
        {
            var taskProjects = dataAccess.GetTaskProjectsForPerson(currentUser.Id);

            if (taskProjects.Count > 0)
            {
                var filteredTasks = new List<TaskDetail>();
                foreach (var task in taskDetails)
                {
                    var id = task.TaskProjectId;
                    if (taskProjects.Where(project => project.Id.Equals(id)).Count() > 0)
                    {
                        filteredTasks.Add(task);
                    }
                }

                return filteredTasks;
            }

            return taskDetails;
        }

        public static List<ExtendedTaskDetail> GetExtendedTaskDetails(IWebStickyNotesDataAccess dataAccess, List<TaskDetail> results)
        {
            var extendedTaskDetails = new List<ExtendedTaskDetail>();
            foreach (var result in results)
            {
                extendedTaskDetails.Add(dataAccess.GetTaskDetail(result.Id));
            }
            return extendedTaskDetails;
        }
    }
}